<?php

/**
 *    Desc
 *
 *    @author    Garbin
 *    @usage    none
 */
class SpecialApp extends MemberbaseApp
{
	var $_special_mod;
    function __construct()
    {
        $this->SpecialApp();
    }
    function SpecialApp()
    {
        parent::__construct();
		$this->check_powers(USER_ADMIN);
		$this->_special_mod =& m('special');
    }
    function index()
    {
		$specials = $this->_special_mod->findAll();
		$this->assign('specials', $specials);
		
        $this->display('special.index.html');
		$this->_curlocal(Lang::get('special_manage'));
    }
	
	function _assign_form()
	{
		$special = array(
			'if_show' => 1,
		);
		$this->assign('special', $special);
	}
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
			
	        $this->display('special.form.html');
			$this->_curlocal(Lang::get('special_manage'), 'index.php?app=special', Lang::get('add_new_special'));
		} else {
			$special_name = trim($_POST['special_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($special_name)) {
				$this->show_warning('special_name_not_empty');
				return;
			}
			if (!$this->_special_mod->unique($special_name)) {
				$this->show_warning('special_name_is_exist');
				return;
			}
			
			$data = array(
				'special_name' => $special_name,
				'description' => $description,
				'if_show' => $if_show
			);
			
			$special_id = $this->_special_mod->add($data);
			if (!$special_id) {
				$this->_error($this->_special_mod->get_error());
				return;
			}
			$this->show_message('add_special_success', Lang::get('special'), 'index.php?app=special');
		}
	}
		
	function edit()
	{
		$special_id = intval($_GET['sid']);
		if (!$special_id) {
			$this->show_warning('special_not_exist');
			return;
		}
		$special = $this->_special_mod->get_info($special_id);
		if (!$special) {
			$this->show_warning('special_not_exist');
			return;
		}
		if (!IS_POST) {
			$this->assign('special', $special);
			
			$this->display('special.form.html');
			$this->_curlocal(Lang::get('special_manage'), 'index.php?app=special', Lang::get('edit_special'));
		} else {
			$special_name = trim($_POST['special_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($special_name)) {
				$this->show_warning('special_name_not_empty');
				return;
			}
			if (!$this->_special_mod->unique($special_name, $special_id)) {
				$this->show_warning('special_name_is_exist');
				return;
			}
			
			$data = array(
				'special_name' => $special_name,
				'description' => $description,
				'if_show' => $if_show
			);
			
			$this->_special_mod->edit($special_id, $data);
			if ($this->_special_mod->get_error()) {
				$this->_error($this->_special_mod->get_error());
				return;
			}
			$this->show_message('edit_special_success', Lang::get('special'), 'index.php?app=special');
		}
	}
	function check_name()
	{
		$special_name = empty($_GET['special_name']) ? null : trim($_GET['special_name']);
		$special_id = empty($_GET['special_id']) ? 0 : intval($_GET['special_id']);
		if (!$special_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_special_mod->unique($special_name, $special_id));
	}
}

?>
